package com.lnj.community.controller;

import com.lnj.community.dao.entity.DiscussPost;
import com.lnj.community.dao.entity.Page;
import com.lnj.community.dao.entity.User;
import com.lnj.community.service.DiscussPostService;
import com.lnj.community.service.LikeService;
import com.lnj.community.service.MessageService;
import com.lnj.community.service.UserService;
import com.lnj.community.utiles.HostHolder;
import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.web.csrf.CsrfToken;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.lnj.community.utiles.CommunityConstant.ENTITY_TYPE_POST;

@Controller
public class HomeController {

    private static final Logger log= LoggerFactory.getLogger(HomeController.class);

    @Autowired
    private DiscussPostService discussPostService;

    @Autowired
    private UserService userService;
    @Autowired
    private MessageService messageService;
    @Autowired
    private LikeService likeService;

    @RequestMapping(path = "/index", method = RequestMethod.GET)
    public String getIndexPage(HttpServletRequest request, Model model, Page page,
                               @RequestParam(name = "orderMode" ,defaultValue = "0") int orderMode) {
        // 方法调用前,SpringMVC会自动实例化Model和Page,并将Page注入Model.
        // 所以,在thymeleaf中可以直接访问Page对象中的数据.
        Page page1 = (Page)model.getAttribute("page");
        System.out.println(page1.getCurrent());
        //获取分页信息 总记录数 总页数 当前页码
        page.setRows(discussPostService.findDiscussPostRows(0));
        //设置路径
        page.setPath("/index?orderMode="+orderMode);

        //获取当前页的数据
        List<DiscussPost> list = discussPostService.findDiscussPosts(0, page.getOffset(), page.getLimit(),orderMode);
        List<Map<String, Object>> discussPosts = new ArrayList<>();
        if (list != null) {
            for (DiscussPost post : list) {
                Map<String, Object> map = new HashMap<>();
                map.put("post", post);
                //根据帖子id查询用户
                User user = userService.findUserById(post.getUserId());
                map.put("user", user);
                //根据帖子id查询点赞数量
                long likeCount = likeService.findEntityLikeCount(ENTITY_TYPE_POST, post.getId());
                map.put("likeCount", likeCount);

                discussPosts.add(map);
            }
        }
        model.addAttribute("discussPosts", discussPosts);
        model.addAttribute("orderMode",orderMode);
        // 查询未读消息数量 (所有会话的未读消息数量)
        User user = HostHolder.getUser();
        if (user!= null) {
            int letterUnreadCount = messageService.findLetterUnreadCount(user.getId(), null);
            model.addAttribute("letterUnreadCount", letterUnreadCount);
        }else {
            model.addAttribute("letterUnreadCount", 0);
        }
        //log.info("discussPosts:"+discussPosts);
//        log.info("你好，log_info");
//        log.warn("你好，log_warn");
//        log.error("你好，log_error");
        return "/index";
    }

    @RequestMapping(path = "/error", method = RequestMethod.GET)
    public String getErrorPage() {
        return "/error/500";
    }

}
